package xnj.lazydog.btcontroller.ControlViews.ControlViews;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import xnj.lazydog.btcontroller.ControlViews.Value;
import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.MyViews.SwitchButton;
import xnj.lazydog.btcontroller.R;

public class Control2AxisStick extends ControlElement {

    public Control2AxisStick (ViewOptions options)
    {
        super(options);


        //options.on_value = 127;
        //options.off_value = -128;

    }

    float THUMB_RADIUS_RATIO = 0.05f;
    float thumb_radius_pix;
    float edge_pix_half;

    RectF mid_rect=new RectF();

    @Override
    public void onSizeChanged() {

//        float w_pix = this.w_pix*0.8f;
//        float h_pix = this.h_pix*0.8f;


        edge_pix = (w_pix+h_pix)*0.01f;

        if(edge_pix>max_edge_pix)
            edge_pix = max_edge_pix;

        if(edge_pix<min_edge_pix)
            edge_pix = min_edge_pix;

        p.setStrokeWidth(edge_pix);

        edge_pix_half = edge_pix*0.5f;

        thumb_radius_pix = (w_pix+h_pix)*THUMB_RADIUS_RATIO;

        rectF.right = w_pix*0.5f;
        rectF.left = -rectF.right;
        rectF.bottom = h_pix*0.5f;
        rectF.top = -rectF.bottom;

        back_rect.left = rectF.left+edge_pix_half;
        back_rect.right = rectF.right-edge_pix_half;
        back_rect.top = rectF.top+edge_pix_half;
        back_rect.bottom = rectF.bottom-edge_pix_half;

        mid_rect.left = rectF.left*0.5f;
        mid_rect.right = rectF.right*0.5f;
        mid_rect.top = rectF.top*0.5f;
        mid_rect.bottom = rectF.bottom*0.5f;


        if(options.on_en)
        {
            if(linkedValues[0]!=null)
                linkedValues[0].set(options.off_value+(options.on_value-options.off_value)*0.5f);
            if(linkedValues[1]!=null)
                linkedValues[1].set(options.off_value+(options.on_value-options.off_value)*0.5f);
        }

        if(linkedValues[0]!=null) {
            pro_x = (linkedValues[0].getFloat() - options.off_value) / (options.on_value - options.off_value);

            if(pro_x>1)
                pro_x = 1;
            else if(pro_x<0)
                pro_x = 0;

        }else
            pro_x = 0.5f;

        if(linkedValues[1]!=null)
        {
            pro_y = (linkedValues[1].getFloat() - options.off_value) / (options.on_value - options.off_value);
            if(pro_y>1)
                pro_y = 1;
            else if(pro_y<0)
                pro_y = 0;
        }else
            pro_y = 0.5f;

        pro_dx = pro_x;
        pro_dy = pro_y;

    }


    float ball_x,ball_y;
    float pro_x,pro_y;
    float pro_dx,pro_dy;
    float pro_dtx,pro_dty;


    @Override
    public void draw(Canvas canvas) {

        p.setStyle(Paint.Style.FILL);
        p.setColor(THEME_GRAY);
        canvas.drawRect(back_rect,p);

        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(edge_pix*0.6f);

        p.setColor(0xffc4c4c4);
        canvas.drawRoundRect(mid_rect,edge_pix,edge_pix,p);

        p.setColor(0xffdddddd);
        canvas.drawLine(rectF.left, 0,rectF.right,0,p);
        canvas.drawLine(0, rectF.top,0,rectF.bottom,p);

        p.setStrokeWidth(edge_pix);
        p.setColor(THEME_WHITE);
        canvas.drawRoundRect(rectF,edge_pix,edge_pix,p);
        if(linkedValues[0]!=null) {
            pro_x = (linkedValues[0].getFloat() - options.off_value) / (options.on_value - options.off_value);

            if(pro_x>1)
                pro_x = 1;
            else if(pro_x<0)
                pro_x = 0;

        }else
            pro_x = 0.5f;

        if(linkedValues[1]!=null)
        {
            pro_y = (linkedValues[1].getFloat() - options.off_value) / (options.on_value - options.off_value);
            if(pro_y>1)
                pro_y = 1;
            else if(pro_y<0)
                pro_y = 0;
        }else
            pro_y = 0.5f;

        pro_dtx = pro_x-pro_dx;
        pro_dty = pro_y-pro_dy;

        if(Math.abs(pro_dtx)>0.005f)
            pro_dx+=pro_dtx*0.3f;
        else
            pro_dx = pro_x;

        if(Math.abs(pro_dty)>0.005f)
            pro_dy+=pro_dty*0.3f;
        else
            pro_dy = pro_y;


        p.setStyle(Paint.Style.FILL);

        ball_x = w_pix*0.8f*(-0.5f+pro_dx);
        ball_y = -h_pix*0.8f*(-0.5f+pro_dy);

        p.setColor(THEME_EDGE);
        canvas.drawCircle(ball_x,ball_y,thumb_radius_pix,p);

        p.setStrokeWidth(edge_pix);
        p.setStyle(Paint.Style.STROKE);
        p.setColor(THEME_WHITE);
        canvas.drawCircle(ball_x,ball_y,thumb_radius_pix,p);

    }

    @Override
    public void clicked(Context context) {


    }

    int di;


    void setValue(float rx,float ry)
    {
        boolean to_trigger=false;


        rx = 0.5f+(rx-0.5f)*1.05f;
        ry = 0.5f+(ry-0.5f)*1.05f;

        ry = 1-ry;

        if(rx>1)
            rx = 1;
        if(rx<0)
            rx = 0;
        if(ry>1)
            ry = 1;
        if(ry<0)
            ry = 0;

        if(linkedValues[0]!=null)
        {
            if(linkedValues[0].type==4)
                linkedValues[0].set(options.off_value+(options.on_value-options.off_value)*rx);
            else
            {
                di = (int)(options.off_value+((options.on_value-options.off_value+0.5f)*rx));
                linkedValues[0].set(di);
            }
            to_trigger = true;
        }

        if(linkedValues[1]!=null)
        {
            if(linkedValues[1].type==4)
                linkedValues[1].set(options.off_value+(options.on_value-options.off_value)*ry);
            else
            {
                di = (int)(options.off_value+((options.on_value-options.off_value+0.5f)*ry));
                linkedValues[1].set(di);
            }
            to_trigger = true;
        }

        if(to_trigger)
            triggerDataEvent();

    }


    @Override
    public void dragged() {

        setValue(rx,ry);

    }



    @Override
    public void showSettingsDialog(Context context) {


        if(linkedValues[0]==null) {
            Toast.makeText(context,"请先链接一个变量",Toast.LENGTH_SHORT).show();
            return;
        }
        final EditText on_edit,off_edit;
        SwitchButton switchButton;
        Button ok;
        TextView info;
        String str;
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View layout = LayoutInflater.from(context).inflate(R.layout.toy_settings_stick,null);
        builder.setView(layout);
        dialog = builder.create();
        if(dialog.getWindow()!=null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        on_edit = layout.findViewById(R.id.on_edit);
        off_edit = layout.findViewById(R.id.off_edit);
        info = layout.findViewById(R.id.button_info);
        ok = layout.findViewById(R.id.button);
        switchButton = layout.findViewById(R.id.switchButton);


        switchButton.thumbOffColor = 0xffdddddd;

        switchButton.setCheckImm(options.on_en);

        switchButton.setOnCheckedChangedListener(new SwitchButton.OnCheckedChangedListener() {
            @Override
            public void onCheckedChanged(boolean isChecked) {
                options.on_en = isChecked;
            }
        });


            if(linkedValues[0].type<4)
                str=(""+(int)(options.on_value));
            else
                str=(""+options.on_value);


        on_edit.setText(str);
        on_edit.setSelection(str.length());


            if(linkedValues[0].type<4)
                str=(""+(int)(options.off_value));
            else
                str=(""+options.off_value);


        off_edit.setText(str);
        off_edit.setSelection(str.length());

        if(linkedValues[0].type<4)
            str = "此控件链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(int)(MIN_VALUES[linkedValues[0].type])+"~"+(int)(MAX_VALUES[linkedValues[0].type]);
        else
            str = "此控件链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(MIN_VALUES[linkedValues[0].type])+"~"+(MAX_VALUES[linkedValues[0].type]);

        info.setText(str);

        initLimitedEdit(on_edit,info,linkedValues[0].type);
        initLimitedEdit(off_edit,info,linkedValues[0].type);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Value value = getNumber(on_edit.getText().toString());
                if(value.type==0)
                {
                    options.on_value = 0;
                }else
                {
                    options.on_value = value.getFloat();
                }

                value = getNumber(off_edit.getText().toString());
                if(value.type==0)
                {
                    options.off_value = 0;

                }else
                {
                    options.off_value = value.getFloat();
                }

                options.threshold = (options.on_value+options.off_value)*0.5f;
                dialog.dismiss();
            }
        });
        dialog.show();
    }



    @Override
    public void released() {

        if(options.on_en)
        {
            if(linkedValues[0]!=null)
                linkedValues[0].set(options.off_value+(options.on_value-options.off_value)*0.5f);
            if(linkedValues[1]!=null)
                linkedValues[1].set(options.off_value+(options.on_value-options.off_value)*0.5f);
            triggerDataEvent();
        }
    }

    @Override
    public void pressed() {

        setValue(rx,ry);

    }

    @Override
    public void zoomed(float sx, float sy) {

    }

}
